<?php
namespace app\home\logic;

use alisms\Sms;
use app\common\logic\Base;
use app\common\logic\Email;
use think\facade\App;
use think\facade\Env;

class Account extends  Base {
    
    /**
     * @dsec 用户信息
     * @param int $userId 用户ID
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($userId) {
        $field = [
            'user_phone AS phone',
            'user_email AS email',
            'user_icon AS icon',
            'user_create_time createTime',
        ];
        $info = App::model('User')
                ->field($field)
                ->where('user_id', '=', $userId)
                ->find();
 
        return $info;
    }
    
    /**
     * @desc 修改用户密码
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function changePasswordAjax($params) {
        $userInfo = App::model('User')
            ->where('user_id', '=', $params['userId'])
            ->find();

        $md5Password = App::model('User')
            ->generatePassword($params['password'], $userInfo['user_salt']);

        if(strcmp($md5Password, $userInfo['user_password']) != 0) {
            $this->errorCode = EC_HOME_USERNAME_PASSWORD_ERROR;
            return false;
        }
    
        if(strcmp($params['newPassword1'], $params['newPassword2']) != 0) {
            $this->errorCode = EC_HOME_CONFIRM_NEW_PASSWORD_ERROR;
            return false;
        }
        $newMd5Password = App::model('User')
            ->generatePassword($params['newPassword2'], $userInfo['user_salt']);
    
        $result =App::model('User')
                ->where('user_id', '=', $params['userId'])
                ->update(['user_password' => $newMd5Password]);
;
        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }
    
    /**
     * @desc 修改邮箱
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function editEmailAjax($params) {
        $userInfo = App::model('User')
            ->where('user_id', '=', $params['userId'])
            ->find();
        
        $md5Password = App::model('User')
            ->generatePassword($params['password'], $userInfo['user_salt']);
        
        if(strcmp($md5Password, $userInfo['user_password']) != 0) {
            $this->errorCode = EC_HOME_USERNAME_PASSWORD_ERROR;
            return false;
        }
        
        //检查验证码
        if(!check_msg_code($params['email'], $params['emailCode'])) {
            $this->errorCode = EC_HOME_VERIFICATION_CODE_ERROR;
            return false;
        }
        
        $result =App::model('User')
            ->where('user_id', '=', $params['userId'])
            ->update(['user_email' => $params['email']]);
        
        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }
    
    /**
     * @desc 获取邮件
     * @param $params
     * @return bool
     * @throws \PHPMailer\PHPMailer\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getEmailCode($params) {
        $userInfo = model('User')->where(['user_email' => $params['email']])->find();
        if($userInfo) {
            $this->errorCode = EC_HOME_USER_EMAIL_ALREADY_EXIST;
            return false;
        }
    
        $code = create_msg_code($params['email']);
        $Email = new Email();
        $result = $Email->sendMail($params['email'], '邮箱注册验证码', '您的邮箱验证码是'. $code.'。5分钟内有效。请勿泄露！');
        if($result === false) {
            $this->errorCode = EC_HOME_EMAIL_VERIFICATION_CODE_SEND_FAIL;
            return false;
        }
        return  true;
    }
    
    /**
     * @desc 修改手机号
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function editPhoneAjax($params) {
        $userInfo = App::model('User')
            ->where('user_id', '=', $params['userId'])
            ->find();
        
        $md5Password = App::model('User')
            ->generatePassword($params['password'], $userInfo['user_salt']);
        
        if(strcmp($md5Password, $userInfo['user_password']) != 0) {
            $this->errorCode = EC_HOME_USERNAME_PASSWORD_ERROR;
            return false;
        }
        
        //检查验证码
        if(!check_msg_code($params['phone'], $params['phoneCode'])) {
            $this->errorCode = EC_HOME_VERIFICATION_CODE_ERROR;
            return false;
        }
        
        $result =App::model('User')
            ->where('user_id', '=', $params['userId'])
            ->update(['user_phone' => $params['phone']]);
        
        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }
    
    /**
     * @desc 获取邮件
     * @param $params
     * @return bool
     */
    public function getPhoneCode($params) {
        $userInfo = model('User')->where(['user_phone' => $params['phone']])->find();
        if($userInfo) {
            $this->errorCode = EC_HOME_USER_PHONE_ALREADY_EXIST;
            return false;
        }
    
        $code = create_msg_code($params['phone']);
        if(in_array($params['phone'], ['17666106339', '16685115339', '15608507167'])) {
            Sms::sendSms($params['phone'], $code);
        }
        return  true;
    }
    
    /**
     * @desc 修改用户图像
     * @param $userId
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function editUserIcon($userId) {
        $generalLogic = model('common/General', 'logic');
        $result = $generalLogic->upload();

        if($result === false) {
            $this->errorCode = EC_UPLOAD_ERROR;
            return false;
        }
        
        $data = App::model('User')
            ->where(['user_id' => $userId])
            ->update(['user_icon' => $result['url']]);
        if($data === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return $result;
    }
}
